package com.hxy.modules.demo.controller;


import com.hxy.modules.bean.response.BaseResponseVo;
import com.hxy.modules.bean.response.Msg;
import com.hxy.modules.common.page.Page;
import com.hxy.modules.common.utils.*;
import com.hxy.modules.demo.entity.MerchantsAuditEntity;
import com.hxy.modules.demo.service.MerchantsAuditService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("demo/audit")
@Controller
@Slf4j
@CrossOrigin
public class MerchantsAuditController {

    @Autowired
    MerchantsAuditService merchantsAuditService;

    /**
     * 请假列表
     * @param merchantsAuditEntity
     * @param request
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("act:model:all")
    public String list(Model model ,MerchantsAuditEntity merchantsAuditEntity, HttpServletRequest request){
        int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
        Page<MerchantsAuditEntity> page = merchantsAuditService.findPage(merchantsAuditEntity, pageNum);
        model.addAttribute("page",page);
        model.addAttribute("merchantsAudit",merchantsAuditEntity);
        return "examine/shenpi";
    }

    /**
     * 请假详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("info")
    @RequiresPermissions("act:model:all")
    @ResponseBody
    public Msg info(String id, HttpServletRequest request){
        if(StringUtils.isEmpty(id)){
           return ResultUtil.error("id不能为空");
        }
        MerchantsAuditEntity merchantsAuditEntity = merchantsAuditService.queryObject(id);
        return ResultUtil.success(merchantsAuditEntity);
    }

    /**
     * 编辑
     * @param merchantsAuditEntity
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @RequiresPermissions("act:model:all")
    @ResponseBody
    public Msg edit(@RequestBody MerchantsAuditEntity merchantsAuditEntity){
        if(StringUtils.isEmpty(merchantsAuditEntity.getId())){
            merchantsAuditService.save(merchantsAuditEntity);
        }else {
            merchantsAuditService.update(merchantsAuditEntity);
        }
        return ResultUtil.success("商品审核编辑成功");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @RequiresPermissions("act:model:all")
    @ResponseBody
    public Msg edit(String id){
        if(merchantsAuditService.delete(id)<1){
            return ResultUtil.error("删除商品审核失败");
        }
        return ResultUtil.success("删除商品审核成功");
    }

    /**
     * 上传头像
     */
    @RequestMapping(value = "/photoUpload", method = RequestMethod.POST)
    @ResponseBody
    public Msg testUploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq, HttpSession session) {
        try {
            String data = UploadFileUtil.UploadFile(req, multiReq);
            log.info("上传成功 ,上传路径:  :" +  data);
            return ResultUtil.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传失败, " + e.getMessage());
            return ResultUtil.error("上传失败");
        }
    }
}
